package com.yinda.dt.produce.app;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.telephony.TelephonyManager;

import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.DataBaseConfig;

import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 * 描述
 * <br/>
 * Created by 小灿 on 2017/2/23.
 */

public class DemoApplication extends Application {

    private LiteOrm liteOrm;

    @Override
    public void onCreate() {
        super.onCreate();

        initJPush();
        initLiteOrm();

    }

    private void initJPush(){
        JPushInterface.setDebugMode(false); 	// 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);     		// 初始化 JPush  JPushUtil.init(getApplicationContext());
    }

    /**
     * 初始化LiteOrm
     */
    private void initLiteOrm() {
        if (liteOrm == null) {
            DataBaseConfig config = new DataBaseConfig(this);
            config.dbVersion = 1;
            config.dbName = "dt.db";
            liteOrm = LiteOrm.newSingleInstance(config);
        }
        liteOrm.setDebugged(false); // open the log
    }

    /**
     * 获取数据库操作框架
     *
     * @return
     */
    public LiteOrm getLiteOrm() {
        return liteOrm;
    }

    /**
     * 获取设备ID
     *
     * @return
     */
    public String getDeviceId() {
        TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        return TelephonyMgr.getDeviceId();
    }

    /**
     * 判断某个服务是否正在运行的方法
     *
     * @param serviceName 是包名+服务的类名（例如：com.baidu.trace.LBSTraceService）
     * @return true代表正在运行，false代表服务没有正在运行
     */
    public boolean isServiceWork(String serviceName) {
        boolean isWork = false;
        ActivityManager myAM = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> myList = myAM.getRunningServices(80);
        if (myList.size() <= 0) {
            return false;
        }
        for (int i = 0; i < myList.size(); i++) {
            String mName = myList.get(i).service.getClassName().toString();
            if (mName.equals(serviceName)) {
                isWork = true;
                break;
            }
        }
        return isWork;
    }

    public String getRoadMeasureDirPath() {
        String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + getApplicationName() + "/roadMeasure";
        return dirPath;
    }

    public String getApplicationName() {
        PackageManager packageManager = null;
        ApplicationInfo applicationInfo;
        try {
            packageManager = getPackageManager();
            applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            applicationInfo = null;
        }
        String applicationName = (String) packageManager.getApplicationLabel(applicationInfo);
        return applicationName;
    }

}
